package com.yanfan.energy.controller;

import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.energy.domain.EnergyPriceHistory;
import com.yanfan.energy.domain.EnergyPriceSetting;
import com.yanfan.energy.domain.statist.PriceStatistics;
import com.yanfan.energy.service.EnergyPriceHistoryService;
import com.yanfan.energy.service.EnergyPriceSettingService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author caijinkang
 * @date 2022年09月28日 11:10
 */
@RestController
@RequestMapping("/energy")
@Api(tags = "能耗费用设置")
public class EnergyPriceSettingController {

    private final Logger log = LoggerFactory.getLogger(WorkshopEnergyController.class);

    @Resource
    private final EnergyPriceSettingService energyPriceSettingService;

    @Resource
    private final EnergyPriceHistoryService energyPriceHistoryService;

    public EnergyPriceSettingController(EnergyPriceSettingService energyPriceSettingService, EnergyPriceHistoryService energyPriceHistoryService) {
        this.energyPriceSettingService = energyPriceSettingService;
        this.energyPriceHistoryService = energyPriceHistoryService;
    }

    @GetMapping("/energy-price-histories")
    @ApiOperation("能源单价设置记录记录")
    public AjaxResult getAllEnergyPriceHistories(@ApiParam(name = "currPage", value = "当前页") @RequestParam int currPage, @ApiParam(name = "pageSize", value = "页条数") @RequestParam int pageSize, @ApiParam(name = "types", value = "类型") @RequestParam(required = false) String types) {
        log.debug("REST request to get all EnergyPriceHistories");
        PageHelper.startPage(currPage, pageSize);
        PageInfo<EnergyPriceHistory> pageInfo = new PageInfo<>(energyPriceHistoryService.selectAllEnergyPriceHistory(types));
        return AjaxResult.success("查询成功", pageInfo);
    }

    @GetMapping("/energy-price-setting")
    @ApiOperation("能源单价设置记录")
    public AjaxResult selectAllEnergyPriceSetting(@ApiParam(name = "currPage", value = "当前页") @RequestParam int currPage, @ApiParam(name = "pageSize", value = "页条数") @RequestParam int pageSize) {
        log.debug("REST request to get all EnergyPriceHistories");
        // PageHelper.startPage(currPage,pageSize);
        PageInfo<EnergyPriceSetting> pageInfo = new PageInfo<>(energyPriceSettingService.selectAllEnergyPriceSetting());
        return AjaxResult.success("查询成功", pageInfo);
    }

    @PostMapping("/energy-price-settings/set-price")
    @ApiOperation("单价设置")
    public AjaxResult updateEnergyPriceSetting(@ApiParam(name = "priceStatistics", value = "单价设置历史对象") @RequestBody List<PriceStatistics> priceStatistics) {
        return energyPriceSettingService.updateEnergyPriceSetting(priceStatistics);
    }

    @GetMapping("/findPriceElectric")
    @ApiOperation("能源单价设置记录")
    public AjaxResult findPriceElectric() {
        log.debug("REST request to get all EnergyPriceHistories");
        Map<String, List<EnergyPriceSetting>> result = energyPriceSettingService.findPriceElectric();
        return AjaxResult.success("查询成功", result);
    }

    @GetMapping("/findAllPriceElectricSet")
    @ApiOperation("能源单价设置查询24小时的四电")
    public AjaxResult findAllPriceElectricSet() {
        log.debug("REST request to get all EnergyPriceHistories");
        List<EnergyPriceSetting> result = energyPriceSettingService.findAllPriceElectricSet();
        return AjaxResult.success("查询成功", result);
    }
}
